import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService, BeActionService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';

@Injectable()
export class GyjghFormFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) { }

  /**
   * 添加数据
   * @remarks 
   * @returns 
   */
  addDatas(): any {
    const context = this.frameContext;
    let id = context.bindingData.getValue(['id'])
    const rep = this.frameContext.repository as BefRepository<any>;
    const datas = this.frameContext.uiState['datas'];
    let ghlyDadas = []
    let ghlymxDadas = []
    debugger
    for (let item of datas) {
      const sqdh = item.data.sqdh
      const sqsl = item.data.sl
      const gyjlx = item.data.gyjlx.gyjlx
      const gyjlxlb = item.data.gyjlx.gyjlx_lb
      const gyjlxlxbh = item.data.gyjlx.gyjlx_lxbh
      const gyjlxlxmc = item.data.gyjlx.gyjlx_lxmc
      context.bindingData.setValue(['lybh'], sqdh, true, true);
      context.bindingData.setValue(['sqsl'], sqsl, true, true);
      context.bindingData.setValue(['ghsl'], sqsl, true, true);
      context.bindingData.setValue(['gyjlx', 'gyjlx'], gyjlx, true, true);
      context.bindingData.setValue(['gyjlx', 'gyjlx_lb'], gyjlxlb, true, true);
      context.bindingData.setValue(['gyjlx', 'gyjlx_lxbh'], gyjlxlxbh, true, true);
      context.bindingData.setValue(['gyjlx', 'gyjlx_lxmc'], gyjlxlxmc, true, true);
      const lymxs = item.data.lymxs
      ghlymxDadas = ghlymxDadas.concat(lymxs);
    }

    for (let mxitem of ghlymxDadas) {
      const tzid = mxitem.tzid.tzid
      const debh = mxitem.tzid.tzid_debh
      const gyjmc = mxitem.tzid.tzid_gyjmc
      const ggxh = mxitem.tzid.tzid_ggxh
      const dw = mxitem.tzid.tzid_dw
      const dnhsdj = mxitem.tzid.tzid_dnhsdj
      const sccj = mxitem.tzid.tzid_sccj
      const gzrq = mxitem.tzid.tzid_gzrq
      const cfwz = mxitem.tzid.tzid_cfwz
      const synx = mxitem.tzid.tzid_synx
      ghlyDadas.push({
        tzid: {
          tzid: tzid,
          tzid_debh: debh,
          tzid_gyjmc: gyjmc,
          tzid_ggxh: ggxh,
          tzid_dw: dw,
          tzid_dnhsdj: dnhsdj,
          tzid_sccj: sccj,
          tzid_gzrq: gzrq,
          tzid_cfwz: cfwz,
          tzid_synx: synx
        }
      })
    }
    rep.batchAppendByPath('/' + id + '/ghmxs', ghlyDadas).subscribe()
  }

  /**
   * 归还类型值变化事件
   * @remarks 
   * @returns 
   */
  ghlxValueChanged(): any {
    const bindingData = this.frameContext.bindingData;
    const mxDatas = bindingData.getValue(['ghmxs']).toJSON();
    debugger
    // 初始化统计变量
    let totalGhsl = 0;
    let totalBfsl = 0;
    for (let item of mxDatas) {
      const lx = item.ghlx;
      if (lx === "0") {
        totalGhsl++;
      } else if (lx === "1") {
        totalBfsl++;
      }
    }
    bindingData.setValue(['ghsl'], totalGhsl);
    bindingData.setValue(['bfsl'], totalBfsl);
  }
}
